package GUI;

import javax.swing.JOptionPane;

import Kernel.*;

public class EditerVehicule extends javax.swing.JFrame {

    private static final long serialVersionUID = 924520015342062430L;
	
	/** Cree une nouvelle instance de EditerVehicule */
    public EditerVehicule() {
        initComponents();
        this.setLocationRelativeTo(this.getParent());
    }

    /**
	 * Initialisation des composants d'AWT.
	 */
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        motifRecherche = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        identifiant = new javax.swing.JTextField();
        capacite = new javax.swing.JTextField();
        vitesse = new javax.swing.JTextField();
        rechercherVehicule = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        editer = new javax.swing.JButton();
        retour = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Editer un vehicule");

        jLabel2.setText("Rechercher par mot cle");

        jLabel3.setText("Vehicule concerne");

        jLabel4.setText("Identifiant ");

        jLabel5.setText("Capacite");

        jLabel6.setText("Vitesse");

        rechercherVehicule.setText("Rechercher");
        rechercherVehicule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rechercherVehiculeActionPerformed(evt);
            }
        });

        jLabel7.setText("places");

        jLabel8.setText("km/h");

        editer.setText("Editer");
        editer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editerActionPerformed(evt);
            }
        });

        retour.setText("Retour");
        retour.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                retourActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(142, 142, 142)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(motifRecherche, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(126, 126, 126)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(vitesse)
                            .addComponent(capacite)
                            .addComponent(identifiant, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(147, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(81, 81, 81)
                        .addComponent(rechercherVehicule))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(retour)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 247, Short.MAX_VALUE)
                        .addComponent(editer)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(motifRecherche, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rechercherVehicule))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(identifiant, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(capacite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vitesse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(editer)
                    .addComponent(retour))
                .addContainerGap())
        );

        pack();
    }

    private void rechercherVehiculeActionPerformed(java.awt.event.ActionEvent evt) {
    	Vehicule v;
    	v = Accueil.serveur.chercherVehicule(this.motifRecherche.getText());
    	if (v!=null){
    		this.capacite.setText(""+v.getCapacite());
    		this.identifiant.setText(v.getDenomination());
    		this.vitesse.setText(""+v.getVitesse());
    	}
    	else{
    		JOptionPane.showMessageDialog(this,
					"La reference n'a pu etre trouvee !", "Pas glop...",
					JOptionPane.ERROR_MESSAGE);
    	}
    }

    private void editerActionPerformed(java.awt.event.ActionEvent evt) {
    	Vehicule v = Accueil.serveur.chercherVehicule(this.motifRecherche.getText());    		
    	if(this.capacite.getText().compareTo("")==0 || this.identifiant.getText().compareTo("")==0 || this.vitesse.getText().compareTo("")==0){
    		JOptionPane.showMessageDialog(this,
					"Les champs ne peuvent etre vide !", "Pas glop...",
					JOptionPane.ERROR_MESSAGE);
    	}
    	else{
    		if(v.getType().compareTo("avion")==0){
    		Accueil.serveur.getVehicules().remove(Accueil.serveur.chercherVehicule(v.getDenomination()));
    		Accueil.serveur.getVehicules().add(new Avion(this.identifiant.getText(),Integer.parseInt(this.capacite.getText()),Integer.parseInt(this.vitesse.getText())));
    		}
    		else if(v.getType().compareTo("bateau")==0){
        		Accueil.serveur.getVehicules().remove(Accueil.serveur.chercherVehicule(v.getDenomination()));
        		Accueil.serveur.getVehicules().add(new Bateau(this.identifiant.getText(),Integer.parseInt(this.capacite.getText()),Integer.parseInt(this.vitesse.getText())));
        		}
    		else if(v.getType().compareTo("bus")==0){
        		Accueil.serveur.getVehicules().remove(Accueil.serveur.chercherVehicule(v.getDenomination()));
        		Accueil.serveur.getVehicules().add(new Bus(this.identifiant.getText(),Integer.parseInt(this.capacite.getText()),Integer.parseInt(this.vitesse.getText())));
        		}
    		else if(v.getType().compareTo("train")==0){
        		Accueil.serveur.getVehicules().remove(Accueil.serveur.chercherVehicule(v.getDenomination()));
        		Accueil.serveur.getVehicules().add(new Train(this.identifiant.getText(),Integer.parseInt(this.capacite.getText()),Integer.parseInt(this.vitesse.getText())));
        		}
    		else if(v.getType().compareTo("generique")==0){
        		Accueil.serveur.getVehicules().remove(Accueil.serveur.chercherVehicule(v.getDenomination()));
        		Accueil.serveur.getVehicules().add(new VehiculeGenerique(this.identifiant.getText(),Integer.parseInt(this.capacite.getText()),Integer.parseInt(this.vitesse.getText())));
        		}
    		this.setVisible(false);
    		Administration ad = new Administration(new Administrateur(Administration.admin.getLogin(), Administration.admin.getPassword(), Accueil.serveur));
    		ad.setVisible(true);
    	}
    }

    private void retourActionPerformed(java.awt.event.ActionEvent evt) {
    	Administration at = new Administration(new Administrateur(Administration.admin.getLogin(), Administration.admin.getPassword(), Accueil.serveur));
		at.setVisible(true);
		this.setVisible(false);
		this.dispose();
    }

    private javax.swing.JTextField capacite;
    private javax.swing.JButton editer;
    private javax.swing.JTextField identifiant;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField motifRecherche;
    private javax.swing.JButton rechercherVehicule;
    private javax.swing.JButton retour;
    private javax.swing.JTextField vitesse;

}
